package com.pureut.storage.controller;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.storage.dto.ProductionMaterialReturnDto;

import com.pureut.storage.export.ProductionMaterialReturnExport;

import com.pureut.storage.service.ProductionMaterialReturnService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.PickingRestDbVo;
import com.pureut.storage.vo.ProductionMaterialReturnVo;

import com.pureut.storage.vo.UpdateStatusByQualityVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/***
 * 生产退料控制层
 * Author:C
 * Date:2022/12/30 9:18
 * @DESC
 */
@RestController
@RequestMapping("/production-material-return")
public class ProductionMaterialReturnController extends BaseController {

    @Resource
    ProductionMaterialReturnService productionMaterialReturnService;


    /**
     * 获取生产退料分页列表
     */
    @RequiresPermissions("warehouse:warehousing:productionReturn:list")
    @GetMapping("/list")
    public TableDataInfo getList(ProductionMaterialReturnVo productionMaterialReturnVo) {
        startPage();
        //查询生产退料列表
        List<ProductionMaterialReturnDto> list = productionMaterialReturnService.getProductionList(productionMaterialReturnVo);
        return getDataTable(list);
    }

    /**
     * 新增生产退料
     */
    @RequiresPermissions("warehouse:warehousing:productionReturn:add")
    @Log(title = "新增生产退料", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody ProductionMaterialReturnVo productionMaterialReturnVo) throws Exception {
        return toAjax(productionMaterialReturnService.addProductionMaterialReturn(productionMaterialReturnVo));
    }

    /**
     * 关结生产退料
     */
    @RequiresPermissions("warehouse:warehousing:productionReturn:closing")
    @Log(title = "关结生产退料", businessType = BusinessType.UPDATE)
    @PostMapping("/close")
    public AjaxResult close(@RequestBody ProductionMaterialReturnVo productionMaterialReturnVo) {
        return toAjax(productionMaterialReturnService.close(productionMaterialReturnVo));
    }

    /**
     * 查看
     */
    @RequiresPermissions("warehouse:warehousing:productionReturn:query")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id) {
        return AjaxResult.success(productionMaterialReturnService.getView(id));
    }

    /**
     * 修改生产退料
     */
    @RequiresPermissions("warehouse:warehousing:productionReturn:edit")
    @Log(title = "修改生产退料", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult updateProductionMaterialReturn(@Validated @RequestBody ProductionMaterialReturnVo productionMaterialReturnVo) {
        return toAjax(productionMaterialReturnService.updateProductionMaterialReturn(productionMaterialReturnVo));
    }

    /**
     * 删除生产退料
     */
    @RequiresPermissions("warehouse:warehousing:productionReturn:remove")
    @Log(title = "删除生产退料", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{idStr}")
    public AjaxResult deleteProductionMaterialReturn(@PathVariable("idStr") String idStr) {
        return toAjax(productionMaterialReturnService.deleteProductionMaterialReturn(idStr));
    }

    /**
     * 导出
     */
    @RequiresPermissions("warehouse:warehousing:productionReturn:export")
    @PostMapping("/productionMessageExport")
    public AjaxResult productionMessageExport(HttpServletResponse response, ProductionMaterialReturnVo productionMaterialReturnVo) throws IOException {
        List<ProductionMaterialReturnExport> infoExport = productionMaterialReturnService.getInfoExport(productionMaterialReturnVo);
        ExcelUtil<ProductionMaterialReturnExport> excelUtil = new ExcelUtil<ProductionMaterialReturnExport>(ProductionMaterialReturnExport.class);
        excelUtil.excelWriter(response, infoExport, "生产退料信息.xlsx");
        return AjaxResult.success();
    }


    /**
     * 生产退料（pda）
     */
    @RequiresPermissions("warehouse:warehousing:productionReturn:warehousing")
    @PostMapping("/productionReturnPDA")
    @Log(title = "生产退料（pda）", businessType = BusinessType.UPDATE)
    public AjaxResult productionReturnPda(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(productionMaterialReturnService.productionReturnPda(pickingRestDbVo));
    }

    /**
     *生产退料拣货撤销
     */
    @PostMapping("/productionCancellation")
    @Log(title = "生产退料拣货撤销", businessType = BusinessType.UPDATE)
    public AjaxResult pickingCancellation(@RequestBody PickingRestDbVo pickingRestDbVo) {
        return toAjax(productionMaterialReturnService.pickingCancellation(pickingRestDbVo));
    }

    /**
     * 生产退料拣货完成
     */
    @PostMapping("/productionCompleted")
    @Log(title = "生产退料拣货完成", businessType = BusinessType.UPDATE)
    public AjaxResult pickingCompleted(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return toAjax(productionMaterialReturnService.pickingCompleted(pickingRestDbVo));
    }

    /**
     * 生产退料交接
     */
    @RequiresPermissions("warehouse:warehousing:productionReturn:handover")
    @PostMapping("/handover")
    @Log(title = "生产退料交接", businessType = BusinessType.UPDATE)
    public AjaxResult handover(@RequestBody PickingRestDbVo pickingRestDbVo) {
        return AjaxResult.success(productionMaterialReturnService.handover(pickingRestDbVo));
    }

    /**
     * 生产退料品质回传状态(fegin调用)
     */
    @PostMapping("/updateProductionMaterialByQuality")
    public AjaxResult updateProductionMaterialByQuality(@RequestBody UpdateStatusByQualityVo updateStatusByQualityVo) {
        return toAjax(productionMaterialReturnService.updateStatusByQuality(updateStatusByQualityVo));
    }
}
